<?php
class NewObjectiveForm extends CFormModel {

	private $meeting = null;
	private $steps = array ();
	private $toSave = array ();

	public function init() {
		$this->meeting = Meetings::model()->findByPk($_GET['id']);
		$this->steps = ProjectSelectedSteps::model()->findAll('project_id=:project_id', array (
			':project_id' => $this->meeting->project->id
		));
	}

	public function getProject() {
		return $this->meeting->project;
	}

	public function getProject_id() {
		return $this->meeting->project->id;
	}

	public function getId() {
		return $this->meeting->id;
	}

	public function getMeeting() {
		return $this->meeting;
	}

	public function getSteps() {
		return $this->steps;
	}

	/*public function getTopics() {
		return $this->steps;
	}*/

	public function rules() {
		return array ();
	}

	public function attributeLabels() {
		return array (
			'adviser_validation' => Yii::t('project', 'Adviser Validation'),
			'owner_validation' => Yii::t('project', 'Owner Validation'),
			'adviser_validation' => Yii::t('comment', 'Comments')
		);
	}

	public function setAttributes($value) {
		parent::setAttributes($value);
		if (is_array($value) && isset ($value['objectives']) && is_array($value['objectives'])) {
			foreach ($value['objectives'] as $id => $items) {
				$topic = $this->findTopic($id);
				if ($topic != null) {
					if ($topic->topic->objective != $items['objective'] || $topic->topic->objective_needs != $items['objective_needs'] || $topic->topic->objective_contacts != $items['objective_contacts']) {
						$this->validateTopic($topic, $items['objective'] == TRUE, $items['objective_needs'], $items['objective_contacts']);
					}
				}
			}
		}
	}

	public function validateTopic($topic, $validated, $needs, $contacts) {
		$queue = new Queue();

		if ($validated) {
			$topic->topic->objective = Constants::TRUE;
			$topic->topic->objective_topic = null;
			$topic->topic->objective_needs = $needs;
			$topic->topic->objective_contacts = $contacts;
			$topic->topic->objective_date = $this->meeting->meeting_date;
			$this->toSave[$topic->topic->id] = $topic;
			$queue->enqueue_all($topic);
			while (!$queue->is_empty()) {
				$t = $queue->dequeue();
				if ($t->canBeObjective()) {
					if ($t->topic->objective != $topic->topic->objective) {
						$t->topic->objective = Constants::TRUE;
						$t->topic->objective_topic_id = $topic->topic->id;
						$this->toSave[$t->id] = $t;
						$queue->enqueue_all($t->children);
					}
					if ($t->topic->objective_needs != $needs) {
						$t->topic->objective_needs = $needs;
						$this->toSave[$t->id] = $t;
						$queue->enqueue_all($t->children);
					}
					if ($t->topic->objective_contacts != $contacts) {
						$t->topic->objective_contacts = $contacts;
						$this->toSave[$t->id] = $t;
						$queue->enqueue_all($t->children);
					}
				}
			}
		} else {
			$topic->topic->objective = Constants::FALSE;
			$topic->topic->objective_topic = null;
			$topic->topic->objective_needs = "";
			$topic->topic->objective_contacts = "";
			$topic->topic->objective_date = null;
			$this->toSave[$topic->topic->id] = $topic;
			$queue->enqueue_all($topic->children);
			while (!$queue->is_empty()) {
				$t = $queue->dequeue();
				if ($t->canBeObjective() && $t->objective_topic == $topic->topic->id) {
					$t->objective = Constants::FALSE;
					$t->objective_topic_id = null;
					$t->objective_needs = "";
					$t->objective_contacts = "";
					$queue->enqueue_all($t->children);
					$this->toSave[$t->id] = $t;
				}
			}
		}
	}

	public function findTopic($id) {
		if (isset ($this->toSave[$id]))
			return $this->toSave[$id];
		else
			return ProjectSelectedTopics::model()->findBySql("select a.* FROM " . ProjectSelectedTopics::tableName() . " a where a.project_topic_id = " . $id);
	}

	/*public function getObjective($id) {
		foreach ($this->objectives as $objective) {
			if ($objective->id == $id) {
				return $objective;
			}
		}

		return null;
	}*/

	public function save() {
		$r = true;
		foreach ($this->toSave as $id => $topic) {
			$r = $r && $topic->topic->save();
		}

		return $r;
	}

}
?>

